<?php
require_once ('/home/n02559308/WWW/Web_Designing/Final/inc/functions.php');

class Shipping_Info
{
	static function GetAll()
	{
		$conn = GetConnection();
		return $conn->query('SELECT * FROM Shipping_Info');
	}
	
	static function Get($id)
	{
		$conn = GetConnection();
		$results = $conn->query("SELECT * FROM Shipping_Info WHERE idShipping_Info=$id");
		$row = $results->fetch_assoc();
		$conn->close();
		return $row;
	}

	
	static function Update($row)
	{
		$conn = GetConnection();
		$sql = "UPDATE Shipping_Info "
			. "SET idShipping_Info='$row[id]',Shipping_Cost='$row[Shipping_Cost]',Shipping_Region='$row[Shipping_Region]',Shipping_Type='$row[Shipping_Type]', Order_Key='$row[Order_Key]' "
			. "WHERE idShipping_Info=$row[id]";
		//echo $sql;
		$results = $conn->query($sql);
		$error->$con->error;
		$conn->close();
		return isset($error) ? array('Server Error'=> $error) : TRUE;
	}
	
	static function Blank()
	{
		return array('FristName'=>null,'LastName'=>null,'created_at'=>null,'updated_at'=>null,'Keyword_id'=>null,'id'=>null,);
	}
	static function Insert(&$row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$conn = GetConnection();
		$results = $conn->query("SELECT max(id) FROM Users");
		$row = $results->fetch_assoc();
		$i = $row['max(id)'];
		$i=$i+1;
		print_r($i);
		$sql = 	"Insert Users (id, FristName, LastName, created_at) "
			.	"Values ($i,'$row2[FristName]','$row2[LastName]',Now())";
			
			
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		if(empty($error))
			$row['id'] = $conn->insert_id;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}

	static function Delete($id)
	{
		$conn = GetConnection();
		$sql = 	"DELETE FROM Shipping_Info WHERE idShipping_Info=$id ";
		echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}

	static function Validate($row)
	{
		$results = array();
		if(empty($row['FristName'])) $results['FristName'] = 'FirstName is required';
		if(empty($row['LastName'])) $results['LastName'] = 'LastName is required';
		
		return count($results) > 0 ? $results : true;
	}
}